package com.yuchen.ishop.web.app.buyer;

import com.yuchen.ishop.entity.BuyerFavorites;
import com.yuchen.ishop.service.buyer.BuyerFavoriteRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(value = "/my/buyer/favorite")
public class BuyerFavoriteAction extends BaseAction{

	@Autowired
    BuyerFavoriteRestClient buyerFavoriteRestClient;
	
	@RequestMapping(value = "index")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        CookieUserInfo userInfo  = cookie2UserInfo(request);
        modelMap.addAttribute("list", buyerFavoriteRestClient.getByBuyerId(userInfo.getId()));
        modelMap.addAttribute("myMenu", "myFavorite");
        return "my/buyer/myFavorite";
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insert(HttpServletRequest request, BuyerFavorites buyerFavorites, ModelMap modelMap) {
        CookieUserInfo userInfo  = cookie2UserInfo(request);
        buyerFavorites.setBuyerId(userInfo.getId());
        buyerFavorites.setCreateTime(getTime());
        buyerFavoriteRestClient.insert(buyerFavorites);
        return "redirect:/my/buyer/favorite/index.htm";
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public String delete(Long id, ModelMap modelMap) {
        buyerFavoriteRestClient.delete(id);
        return "redirect:/my/buyer/favorite/index.htm";
    }

}
